package com.cat.dataStructure03;

import java.util.*;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/design-a-food-rating-system/
 * @create 2025/11/12 16:53
 * @since JDK17
 */
class FoodRatings {
    class Food {
        String foods, cuisines;
        int rating;

        public Food(String foods, String cuisines, int rating) {
            this.foods = foods;
            this.cuisines = cuisines;
            this.rating = rating;
        }
    }
    Map<String, PriorityQueue<Food>> map = new HashMap<>();
    Map<String, Food> foodMap = new HashMap<>();
    public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
        for (int i = 0; i < foods.length; i++) {
            var food = new Food(foods[i], cuisines[i], ratings[i]);
            map.computeIfAbsent(cuisines[i], x -> new PriorityQueue<>((a, b) -> {
                if (a.rating != b.rating) {
                    return b.rating - a.rating;
                }
                return a.foods.compareTo(b.foods);
            })).add(food);
            foodMap.put(foods[i], food);
        }
    }

    public void changeRating(String food, int newRating) {
        Food f = foodMap.get(food);
        f.rating = newRating;
        PriorityQueue<Food> queue = map.get(f.cuisines);
        if (queue != null) {
            queue.remove(f);
            queue.add(f);
        }
    }

    public String highestRated(String cuisine) {
        return Objects.requireNonNull(map.get(cuisine).peek()).foods;
    }
}
